package com.infore.selectbrowserphoto;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.infore.photobrowseselect.BrowserPhotoFragment;
import com.infore.photobrowseselect.SelectPhotoFragment;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;

/**
 * Main Activity
 * 
 * @author dingxinlin
 * @date 2018-4-20
 */
public class MainActivity extends AppCompatActivity {

    private ArrayList<String> uriList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取权限
        obtainPermissions();

        // 构建图片资源
        buildMockUriData();

        // 图片浏览
        Fragment browserPhotoFragment = BrowserPhotoFragment.newInstance(uriList, 3);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.browser_container, browserPhotoFragment, BrowserPhotoFragment.class.getSimpleName())
                .commitAllowingStateLoss();

        // 图片选择
        Fragment selectPhotoFragment = SelectPhotoFragment.newInstance(50, 3);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.select_container, selectPhotoFragment, SelectPhotoFragment.class.getSimpleName())
                .commitAllowingStateLoss();

    }

    /**
     * 获取权限
     */
    private void obtainPermissions() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions
                .requestEachCombined(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA)
                .subscribe(permission -> {
                    if (permission.granted) {
                        // 同意
                        Log.d("dxl", "同意" + permission.name);
                    } else if (permission.shouldShowRequestPermissionRationale) {
                        // 禁止
                        Log.d("dxl", "禁止" + permission.name);
                    } else {
                        // 禁止后不再询问
                        // 需要去设置中打开
                        Log.d("dxl", "禁止后不再询问" + permission.name);
                    }
                });
    }

    /**
     * 构建模拟数据
     */
    private void buildMockUriData() {
        uriList = new ArrayList<>();
        uriList.add("http://static.fdc.com.cn/avatar/sns/1486263782969.png");
        uriList.add("http://static.fdc.com.cn/avatar/sns/1485055822651.png");
        uriList.add("http://static.fdc.com.cn/avatar/sns/1486194909983.png");
        uriList.add("http://static.fdc.com.cn/avatar/sns/1486194996586.png");
        uriList.add("http://static.fdc.com.cn/avatar/sns/1486195059137.png");
        uriList.add("http://static.fdc.com.cn/avatar/sns/1486173497249.png");
        uriList.add("http://static.fdc.com.cn/avatar/sns/1486173526402.png");
        uriList.add("http://static.fdc.com.cn/avatar/sns/1486173639603.png");
        uriList.add("http://static.fdc.com.cn/avatar/sns/1486172566083.png");

    }

}
